package com.ctms.system.dao;

import com.ctms.system.domain.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface UserMapper {

	@Select("select * from sys_user where user_id = #{userId}")
    UserDO get(Long user_id);
	
	@Select("<script>" +
	"select * from sys_user " +
			"<where>" + 
		  		 /* "<if test=\"userId != null and userId != ''\">"+ "and user_id = #{userId} " + "</if>" +
		  		  "<if test=\"username != null and username != ''\">"+ "and username = #{username} " + "</if>" +
		  		  "<if test=\"password != null and password != ''\">"+ "and password = #{password} " + "</if>" +
		  		  "<if test=\"email != null and email != ''\">"+ "and email = #{email} " + "</if>" +
		  		  "<if test=\"name != null and name != ''\">"+ "and name = #{name} " + "</if>" +
		  		  "<if test=\"status != null and status != ''\">"+ "and status = #{status} " + "</if>" +
		  		  "<if test=\"firstName != null and firstName != ''\">"+ "and first_name = #{firstName} " + "</if>" +
		  		  "<if test=\"lastName != null and lastNamee != ''\">"+ "and last_name = #{lastName} " + "</if>" +*/
		  		 /* "<if test='name!=null'> and name like CONCAT('%',#{name},'%')</if>"+*/
			      "<if test=\"username != null and username != ''\">"+ "and username = #{username} " + "</if>"+
		  			"</where>"+ 
			"<if test=\"offset != null and limit != null\">"+
			"limit #{offset}, #{limit}" + 
			"</if>"+
			"</script>")
	List<UserDO> list(Map<String, Object> param);

	@Select("<script>" +
			"select * from sys_user " +
			"<where>" +
			     "<if test='name!=null'> and name like CONCAT('%',#{name},'%')</if>"+
			"</where>"+
			"ORDER BY identity ASC "+
			"<if test=\"offset != null and limit != null\">"+
			"limit #{offset}, #{limit}" +
			"</if>"+
			"</script>")
	List<UserDO> selectByName(Map<String, Object> param);

	@Select("<script>" +
	"select count(*) from sys_user " +
			"<where>" +
			"<if test=\"name != null and name != ''\">"+ "and name = #{name} " + "</if>" +
            "</where>"+
			"</script>")
	int count(Map<String, Object> map);
	
	@Options(useGeneratedKeys = true, keyProperty = "userId")
	@Insert("insert into sys_user (`user_id`,`name`,`username`, `password`, `email`, `status`,`mobile`," +
			"`user_id_create`,`gmt_create`,`gmt_modified`,`identity`,`sex`,`teacher_from`,`teacher_college`," +
			"`teacher_company`,`teacher_description`,`student_class`,`student_course`)"
			+ "values (#{userId}, #{name}, #{username}, #{password}, #{email}, #{status}, #{mobile}," +
			"#{userIdCreate}, #{gmtCreate}, #{gmtModified}, #{identity}, #{sex}, #{teacherFrom}, " +
			"#{teacherCollege}, #{teacherCompany}, #{teacherDescription},#{studentClass},#{studentCourse})")
	int save(UserDO obj);
	
	@Update("<script>"+
			"update sys_user " +
					"<set>" + 
		            "<if test=\"userId != null\">`user_id` = #{userId}, </if>" + 
                    "<if test=\"username != null\">`username` = #{username}, </if>" + 
                    "<if test=\"password != null\">`password` = #{password}, </if>" + 
                    "<if test=\"email != null\">`email` = #{email}, </if>" + 
                    "<if test=\"name != null\">`name` = #{name}, </if>" +
                    "<if test=\"status != null\">`status` = #{status}, </if>" + 
                    "<if test=\"mobile != null\">`mobile` = #{mobile}, </if>" +
                    "<if test=\"userIdCreate != null\">`user_id_create` = #{userIdCreate}, </if>" +
					"<if test=\"gmtCreate != null\">`gmt_create` = #{gmtCreate}, </if>" +
					"<if test=\"gmtModified != null\">`gmt_modified` = #{gmtModified}, </if>" +
					"<if test=\"identity != null\">`identity` = #{identity}, </if>" +
					"<if test=\"sex != null\">`sex` = #{sex}, </if>" +
					"<if test=\"teacherFrom != null\">`teacher_from` = #{teacherFrom}, </if>" +
					"<if test=\"teacherCollege != null\">`teacher_college` = #{teacherCollege}, </if>" +
					"<if test=\"teacherCompany != null\">`teacher_company` = #{teacherCompany}, </if>" +
					"<if test=\"teacherDescription != null\">`teacher_description` = #{teacherDescription}, </if>" +
					"<if test=\"studentClass != null\">`student_class` = #{studentClass}, </if>" +
					"<if test=\"studentCourse != null\">`student_course` = #{studentCourse}, </if>" +
          					"</set>" + 
					"where user_id = #{userId}"+
			"</script>")
	int update(UserDO obj);
	
	@Delete("delete from sys_user where user_id =#{userId}")
	int remove(Long user_id);
	
	@Delete("<script>"+ 
			"delete from sys_user where user_id in "+
			"<foreach collection=\"list\" index=\"i\" open=\"(\" separator=\",\" close=\")\" item=\"item\"  >#{item}</foreach>"+
			"</script>")
	int batchRemove(List<Long> list);

	@Select("select * from sys_user where username = #{username}")
    UserDO findByName(String username);

	public List<String> getRoleName(Long id);

	//查询所有的用户
	List<UserDO> selectAllUser();

	//通过id查询用户信息
	UserDO selectUserById(Integer userId);
	//管理员个人中心
	int updateAdmin(UserDO userDO);

	//通过tid查询用户信息
	List<UserDO> selectUserByTid(Integer tid);

}
